package com.itts.hk.map;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 高德地图判断
 */
public class GaoDe {
    private static boolean IsPtInPoly(double ALon, double ALat, List<GpsBO> APoints) {
        int iSum = 0, iCount;
        double dLon1, dLon2, dLat1, dLat2, dLon;
        if (APoints.size() < 3)
            return false;
        iCount = APoints.size();
        for (int i = 0; i < iCount - 1; i++) {
            if (i == iCount - 1) {
                dLon1 = APoints.get(i).getLon();
                dLat1 = APoints.get(i).getLat();
                dLon2 = APoints.get(0).getLon();
                dLat2 = APoints.get(0).getLat();
            } else {
                dLon1 = APoints.get(i).getLon();
                dLat1 = APoints.get(i).getLat();
                dLon2 = APoints.get(i + 1).getLon();
                dLat2 = APoints.get(i + 1).getLat();
            }
            //以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);

                    // 如果交点在A点左侧（说明是做射线与边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon)
                        iSum++;
                }
            }
        }
        if (iSum % 2 != 0)
            return true;
        return false;
    }

    @Test
    public void test1() {
        double px = 120.20811659294394;
        double py = 31.603822499513253;
        List<GpsBO> list = new ArrayList<>();//区域范围
        list.add(new GpsBO(120.208722, 31.604245));
        list.add(new GpsBO(120.212649, 31.605031));
        list.add(new GpsBO(120.212005, 31.599164));
        list.add(new GpsBO(120.208873, 31.599384));
        boolean b = IsPtInPoly(px, py, list);
        System.out.println(b);
    }

}
